﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 24.04.2008
 * Zeit: 15:27
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Gamadus.Core.Resources
{
	public enum EventType
	{
		None,
		Step,
		Create,
		Draw
	}
	
	/// <summary>
	/// Description of Object.
	/// </summary>
	public class Object : Resource
	{
		List<Event> events = new List<Event>();
		
		public Object(int id, string name, Project project):base(id, name, project)
		{
			this.m_type = Type.Object;
			m_settings = new Settings();
			Settings["visible"] = true;
			Settings["solid"] = false;
			Settings["persistent"] = false;
			Settings["depth"] = 0;
		}
		
		public Event AddEvent(EventType type)
		{
			Event evt = new Event(type);
			events.Add(evt);
			return evt;
		}
		
		public void RemoveEvent(Event evt)
		{
			events.Remove(evt);
		}
		
		public void RemoveEvent(EventType type)
		{
			Event evtRemove = null;
			foreach(Event evt in events)
			{
				if(evt.Type == type)
				{
					evtRemove = evt;
					break;
				}
			}
			if(evtRemove == null)
				return;
			events.Remove(evtRemove);
		}
		
		public override void ReadXml(XElement element)
		{
			foreach(var eEvent in element.Element("events").Elements("event"))
			{
				var evt = AddEvent((EventType)Enum.Parse(typeof(EventType),(string)eEvent.Attribute("type"),true));
				foreach(var eAction in eEvent.Elements("action"))
				{
					var action = evt.AddAction(m_project.ActionMgr.FindPrototypeByName((string)eAction.Attribute("name")));
					foreach(var param in eAction.Elements())
					{
						//TODO: read parameter and add to action
					}
				}
			}
		}
//		public override void ReadXml(XmlElement element)
//		{
//			if(element.GetElementsByTagName("Events").Count == 0)
//				return;
//			
//			XmlElement eventChild = (XmlElement)element.GetElementsByTagName("Events")[0];
//			foreach(XmlElement child in eventChild.ChildNodes)
//			{
//				if(!child.HasAttribute("Type") || child.ChildNodes.Count == 0)
//					continue;
//				
//				EventType type = (EventType)Enum.Parse(typeof(EventType), child.Attributes["Type"].InnerText);
//				Event evt = AddEvent(type);
//				foreach(XmlElement actionChild in child.ChildNodes)
//				{
//					if(actionChild.Name != "Action" || !actionChild.HasAttribute("Name"))
//						continue;
//					
//					ActionPrototype prototype = ActionSetMgr.Instance.FindPrototypeByName(actionChild.Attributes["Name"].InnerText);
//					if(prototype == null)
//						continue;
//					
//					Action act = new Action(prototype);
//					evt.Actions.Add(act);
//					foreach(XmlElement paramChild in actionChild.ChildNodes)
//					{
//						if(paramChild.Name != "Param" || !paramChild.HasAttribute("Name") || !paramChild.HasAttribute("Type"))
//							continue;
//						
//						ActionParam param = act.ParamList[paramChild.Attributes["Name"].InnerText];
//	
//						if(param.GetType().ToString() != "Gamadus.Designer."+paramChild.Attributes["Type"].InnerText)
//							continue;
//						
//						
//						param.ReadXml(paramChild);
//					}
//				}
//			}
//		}
//		
//		public override void WriteXml(XmlTextWriter xml)
//		{
//			xml.WriteStartElement("Events");
//			foreach(Event evt in events)
//			{
//				xml.WriteStartElement("Event");
//				xml.WriteAttributeString("Type", evt.Type.ToString());
//				foreach(Action act in evt.Actions)
//				{
//					xml.WriteStartElement("Action");
//					xml.WriteAttributeString("Name", act.Prototype.Name);
//					foreach(KeyValuePair<string, ActionParam> pair in act.ParamList)
//					{
//						xml.WriteStartElement("Param");
//						xml.WriteAttributeString("Name", pair.Key);
//						xml.WriteAttributeString("Type", pair.Value.GetType().ToString().Substring(17));
//						pair.Value.WriteXml(xml);
//						xml.WriteEndElement();
//					}
//					xml.WriteEndElement();
//				}
//				xml.WriteEndElement();
//			}
//			xml.WriteEndElement();
//		}
	}
	
	public class Event
	{
		EventType m_type;
		public EventType Type
		{
			get
			{
				return m_type;
			}
		}
		List<Action> m_actions = new List<Action>();
		
		public IEnumerable<Action> Actions
		{
			get
			{
				return m_actions;
			}
		}
		
		public string Name
		{
			get
			{
				return Type.ToString();
			}
		}
		
		public Event(EventType type)
		{
			m_type = type;
		}
		
		public Action AddAction(ActionPrototype prototype)
		{
			var action = new Action(prototype);
			m_actions.Add(action);
			return action;
		}
		
		public void AddAction(Action action)
		{
			m_actions.Add(action);
		}
	}
	
//	public class Instance
//	{
//		Gamadus.Designer.Object obj
//		{
//			get
//			{
//				return (Object)ResourceMgr.Instance.GetResourceByID(id);
//			}
//		}
//		int id;
//		Point position;
//		public Instance(Gamadus.Designer.Object obj, Point position) : this(obj.ID, position) {}
//		
//		public Instance(int id, Point position)
//		{
//			this.position = position;
//			this.id = id;
//		}
//		
//		public Point Position
//		{
//			get
//			{
//				return position;
//			}
//		}
//		
//		public int ObjectID
//		{
//			get
//			{
//				return id;
//			}
//		}
//		public Instance Clone()
//		{
//			return (Instance)this.MemberwiseClone();
//		}
//	}
}
